package com.m1905hd.user.web.controller

import com.m1905hd.common.global.JSONCode
import com.m1905hd.common.util.DateUtils
import com.m1905hd.common.util.Page
import com.m1905hd.user.service.NewsService
import com.mongodb.BasicDBObject
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam

@Controller
@RequestMapping("news")
class NewsController {


    @Autowired
    NewsService newsService

    @RequestMapping("list")
    def showList() {
        return "news/newsList"
    }

    @RequestMapping(value = "/list.json")
    def list(
            @RequestParam(defaultValue = "15") int limit, @RequestParam(defaultValue = "0") int pageIndex,
            @RequestParam(defaultValue = "0") int id, String title, String startDate, String endDate, String auth
    ) {
        pageIndex = pageIndex + 1
        def query = new BasicDBObject()
        if (id != 0) {
            query.append("_id", id)
        }
        if (!StringUtils.isEmpty(auth)) {
            query.append("auth", auth)
        }
        if (!StringUtils.isEmpty(title)) {
            query.append("title", ~/${title}/)
        }
        if (!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) {
            query.append("created", [$gte: DateUtils.parseDate(startDate).getTime(), $lte: DateUtils.parseDate(endDate).getTime()] as BasicDBObject)
        }
        Page page = this.newsService.list(query, new BasicDBObject("created", -1), null, pageIndex, limit)
        [results: page.count, rows: page.data]
    }

    @RequestMapping("addNews/{id}")
    def addNews(@PathVariable("id") int id, Model model) {
        def obj = [title: "", auth: "", content: "", status: 0] as BasicDBObject
        if (id != 0) {
            obj = newsService.findOne(id)
        }
        model.addAttribute("news", obj)
        return "news/addNews"
    }


    @RequestMapping(value = "edit", method = RequestMethod.POST)
    def edit(
            @RequestParam(defaultValue = "0") int id, String title, String auth, String content, int status, String info,int type) {
        def obj = [title: title, auth: auth, content: content, status: status, info: info,type:type] as BasicDBObject
        if (id == 0) {
            obj.created = System.currentTimeMillis()
            id = this.newsService.getCurrentId()
        }
        this.newsService.update(id, obj)
        return "redirect:list"
    }

    @RequestMapping("delete.json")
    def del(String ids) {
        ids.split(",").each {
            if (!StringUtils.isEmpty(it)) {
                this.newsService.del(Integer.parseInt(it))
            }
        }
        return [code: JSONCode.SUCCESS.code]
    }

}
